package com.woniu.user.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.user.controller.form.AddCommentForm;
import com.woniu.user.model.Account;
import com.woniu.user.model.Comment;
import com.woniu.user.dao.CommentDao;
import com.woniu.user.service.AccountService;
import com.woniu.user.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author Silas
 * @since 2023-02-15
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentDao, Comment> implements CommentService {

    @Resource
    private CommentDao commentDao;
    @Resource
    private AccountService accountService;

    @Override
    public Boolean reply(Integer id, String content) {
        Comment comment = getById(id);
        Assert.notNull(comment, "非法请求,评论不存在");
        comment.setContent(content);
        comment.setReplyer("场馆");
        comment.setTime(LocalDateTime.now());
        return updateById(comment);
    }

    @Override
    public List<Comment> query(Integer articleId) {
        List<Comment> comments = commentDao.selectList(new QueryWrapper<Comment>().eq("article_id", articleId));
        return comments;
    }

    /**
     * 添加评论
     *
     * @param userId
     * @param addCommentForm
     * @return
     */
    @Override
    public Boolean addComment(Integer userId, AddCommentForm addCommentForm) {
        Account account = accountService.getById(userId);
        String content = addCommentForm.getContent();
        Integer articleId = addCommentForm.getArticleId();
        Assert.notBlank(content, "非法请求,评论内容为空");
        Assert.notNull(articleId, "非法请求,文章id为空");
        Comment comment = new Comment();
        comment.setArticleId(articleId);
        comment.setContent(content);
        comment.setTime(LocalDateTime.now());
        //获取当前用户场馆的名称
        comment.setReplyer(account.getNickname());
        return save(comment);
    }
}
